/**
 * Created with JetBrains PhpStorm.
 * User: ishowshao
 * Date: 13-4-22
 */

var host = 'http://localhost:8000';
/**
 * main logic start
 */
require(['jquery', 'app/view/Grid', 'app/view/LoginForm', 'app/view/OnlineUsers'], function ($, Grid, LoginForm, OnlineUsers) {
    $(function () {
        var onlineUsers = new OnlineUsers();
        onlineUsers.render('#bp-online-users');

        $('#bp-logout').click(function () {
            $.get(host + '/logout', function (result) {
                if (result.success) {
                    onLogoutSuccess();
                }
            }, 'json');
        });

        var onLoginSuccess = function (username) {
            $('#bp-login-form').hide();
            $('#bp-self').show().text(username);
            startListen();
            getOnlineUsers(function (users) {
                onlineUsers.refresh(users);
            });
        };

        var onLogoutSuccess = function () {
            console.log('logout');
            $('#bp-login-form').show();
            $('#bp-self').show().text('');
            stopListen();
        };

        /**
         * @param {Function} callback
         */
        var getOnlineUsers = function (callback) {
            $.get(host + '/online-users', function (result) {
                callback(result.data);
            }, 'json');
        };

        //检查是否已经登陆
        $.get(host + '/is-login', function (result) {
            if (result.success) {
                onLoginSuccess(result.username);
            } else {
                $('#bp-login').click(function (e) {
                    e.preventDefault();
                    var username = $('#bp-username').val().trim();
                    var password = $('#bp-password').val();
                    if (username === '') {
                        alert('用户名不能空哦');
                    } else {
                        $.post(host + '/login', {username: username, password: password}, function (result) {
                            if (result.success) {
                                onLoginSuccess(username);
                            }
                        }, 'json');
                    }
                });
            }
        }, 'json');

        var listen = function () {
            if (listen.enable) {
                $.get(host + '/message?t=' + new Date, function (message) {
                    console.log('onMessage');
                    if (message.type == 'user' && message.code == 1) {
                        onlineUsers.add(message.data);
                    } else if (message.type == 'user' && message.code == 2) {
                        onlineUsers.remove(message.data);
                    } else if (message.type == 'timeout') {

                    }
                    listen();
                }, 'json');
            }
        };
        listen.enable = false;

        var startListen = function () {
            listen.enable = true;
            listen();
        };
        var stopListen = function () {
            listen.enable = false;
        };

        //注册
        var regForm = $('#bp-register-form');
        regForm.find('button').click(function (e) {
            e.preventDefault();
            var username = regForm.find('input[type=text]').val().trim();
            var password = regForm.find('input[type=password]').val();
            if (username == '') {
                alert('用户名不能空');
            } else {
                $.post(host + '/register', {username: username, password: password}, function (result) {
                    if (result.success) {
                        alert('注册成功');
                    }
                }, 'json');
            }
        });
    });
});